/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        'export':false,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true
    },
    {
        id: 'id',
        title: '客户ID',
        type: 'int',
        columnClass: 'text-center width-50',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'eq'

    },
    {
        id: 'userName',
        title: '用户名',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'realName',
        title: '姓名',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    // {
    //     id: 'nickName',
    //     title: '昵称',
    //     type: 'string',
    //     headerClass: 'text-center',
    //     columnClass: 'text-center',
    //     fastSort: false,
    //     fastQuery: true,
    //     fastQueryType: 'lk'
    // },
    {
        id: 'registerTime',
        title: '注册时间',
        type: 'date',
        format:'yyyy-MM-dd',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'range'
    },
    {
        id: 'mobilePhone',
        title: '电话',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center width-100',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'v.userName',
        title: '邀请人',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
    },
    {
        id: 'password',
        title: '密码',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        hide:true
    },
    {
        id: 'inviteCode',
        title: '邀请码',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        hide:false
    },
    {
        id: 'userType',
        title: '类型',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (record.userType == 1){
                return "客户";
            }
            if (record.userType == 2){
                return "投资人";
            }
            if (record.userType == 3){
                return "次销售";
            }
            if (record.userType == 4){
                return "一级代理商";
            }
            if (record.userType == 5){
                return "二级代理商";
            }
            if (record.userType == 6){
                return "三级代理商";
            }
            if (record.userType == 7){
                return "代理商销售";
            }
            if (record.userType == 0){
                return "平台";
            }
        }
    },
    {
        id: 'resource',
        title: '注册来源',
        type: 'number',
        headerClass: 'text-center',
        columnClass: 'text-center width-200',
        fastSort: false,
        hide:false
    },
    {
        id: 'addInvest',
        title: '投资人',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var pwdHtml = "<a data-target='#addModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "'><i class='fa fa-edit'></i>&nbsp;添加投资人&nbsp;</a>";
            var html = "";
            if (record.userType == 1) {
                // 判断是否有编辑权限
                if ($("#addInvest") && $("#addInvest").val() == 1) {
                    html += pwdHtml;
                }
            }
            return html;
        }
    },
    {
        id: 'addseller',
        title: '销售',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var pwdHtml = "<a data-target='#addModal2' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "'><i class='fa fa-edit'></i>&nbsp;添加销售&nbsp;</a>";

            var html = "";
            if (record.userType == 1) {
                // 判断是否有编辑权限
                if ($("#addSeller") && $("#addSeller").val() == 1) {
                    html += pwdHtml;
                }
            }
            return html;
        }
    },
    {
        id: 'reset',
        title: '密码',
        type: 'string',
        columnClass: 'text-center width-200',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var pwdHtml = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;重置&nbsp;</a>";

            var html = "";
            // 判断是否有编辑权限
            if ($("#pwdPermi") && $("#pwdPermi").val() == 1) {
                html += pwdHtml;
            }
            return html;
        }
    },
    // {
    //     id: 'reset',
    //     title: '修改邀请人ID',
    //     type: 'string',
    //     columnClass: 'text-center width-200',
    //     fastSort: false,
    //     resolution: function (value, record, column, grid, dataNo, columnNo) {
    //         var pwdHtml = "<a data-target='#editModal2' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;修改邀请人ID&nbsp;</a>";
    //
    //         var html = "";
    //         // 判断是否有编辑权限
    //         if ($("#cgInvest") && $("#cgInvest").val() == 1) {
    //             html += pwdHtml;
    //         }
    //         return html;
    //     }
    // },
    {
        id: 'password',
        title: '查看',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var look = "<a href='" + ncGlobal.adminRoot + "user/look?id=" + record.id + "' class='btn btn-sm btn-primary m-r-10' data-toggle='modal'><i class='fa fa-edit'></i>&nbsp;查看&nbsp;</a>";
            var html = "";
            // 判断是否有编辑权限
            if ($("#userLook") && $("#userLook").val() == 1) {
                html += look;
            }
            return html;
        }
    },
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'user/list.json',
    exportFileName: '客户列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int:["id","inviteCode"],Timestamp:["registerTime"]}
};

var grid = $.fn.DtGrid.init(dtGridOption);
grid.fastQueryParameters = new Object();

//排序
grid.sortParameter.columnId = ['asc_id'];
// grid.sortParameter.sortType = 1;
//列表结束

//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {

        //添加投资人对话框初始化
        $("#addModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#addForm").psly().reset();
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            var datano = button.data('no');
            var gridData = grid.sortOriginalDatas[datano];
            var modal = $(this);

            modal.find("[name='id']").val(gridData.id);
            modal.find("[name='mobilePhone']").val(gridData.mobilePhone);
            modal.find("[name='userName']").val(gridData.userName);
            modal.find("[name='realName']").val(gridData.realName);
            $("#password").val(gridData.password);
            $("#feeScale").val('');
        });
        //新增销售对话框初始化
        $("#addModal2").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#addForm").psly().reset();
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            var datano = button.data('no');
            var gridData = grid.sortOriginalDatas[datano];
            var modal = $(this);

            modal.find("[name='id']").val(gridData.id);
            modal.find("[name='mobilePhone']").val(gridData.mobilePhone);

            $("#user_name").val(gridData.userName);
            $("#real_name").val(gridData.realName);
            $("#fee_scale").val('');

            $(".status").bootstrapSwitch('state', true);

            $("#pass").val(gridData.password);
        });
        //编辑对话框初始化
        $("#editModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#editForm").psly().reset();
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];
            $("#id").val(gridData.id);
            $("#user_id").val(gridData.id);
        });
        //编辑对话框初始化2
        $("#editModal2").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#editForm").psly().reset();
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];

            $("#userId").val(gridData.id);
            $("#resource").val(gridData.resource);
            modal.find("[name='inviteId']").val('');
        });

        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['lk_user_name_or_like_real_name_or_like_mobile_phone'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
    }

    /**
     * 密码重置
     */
    function resetPwd(id) {
        var tpl = '您确定要重置密码吗?'
        $.ncConfirm({
            url: ncGlobal.adminRoot + "/user/pwdReset.json",
            data: {
                id: id
            },
            content: tpl
        });
    }

    //外部可调用
    return {
        bindEvent: _bindEvent,
        resetPwd: resetPwd
    }
}();
//操作处理结束

$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.bindEvent();
});